/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mikepenz.actionitembadge.library.utils;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrSet;
import ohos.agp.components.ComponentState;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.element.StateElement;
import ohos.app.Context;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by mikepenz on 02.07.15.
 */
public class BadgeDrawableBuilder {
    private int mColor = 0;
    private int mColorPressed = 0;
    private int mCorners = -1;
    private int mStroke = -1;
    private int mStrokeColor = 0;

    /***
     *
     * 构造器
     ***/
    public BadgeDrawableBuilder() {
    }

    /**
     * 正常状态下颜色
     *
     * @param color
     * @return BadgeDrawableBuilder
     **/
    public BadgeDrawableBuilder color(int color) {
        this.mColor = color;
        return this;
    }

    /**
     * 按压时的颜色
     *
     * @param colorPressed 按压时的颜色
     * @return BadgeDrawableBuilder
     **/
    public BadgeDrawableBuilder colorPressed(int colorPressed) {
        this.mColorPressed = colorPressed;
        return this;
    }

    /**
     * 设置corner
     *
     * @param corners 设置角度
     * @return BadgeDrawableBuilder
     **/
    public BadgeDrawableBuilder corners(int corners) {
        this.mCorners = corners;
        return this;
    }

    /**
     * 设置stroke
     *
     * @param stroke 设置stroke
     * @return BadgeDrawableBuilder
     **/
    public BadgeDrawableBuilder stroke(int stroke) {
        this.mStroke = stroke;
        return this;
    }

    /**
     * 设置strokeColor
     *
     * @param strokeColor 设置stroke颜色
     * @return BadgeDrawableBuilder
     **/
    public BadgeDrawableBuilder strokeColor(int strokeColor) {
        this.mStrokeColor = strokeColor;
        return this;
    }

    /**
     * 获得创建StateElement
     *
     * @param attrSet 资源set
     * @param context 上下文
     * @return StateElement
     **/
    public StateElement build(AttrSet attrSet, Context context) {
        StateElement mBackgroundColors;
        RgbColor mDEFAULT_SELECTED_COLOR = RgbColor.fromArgbInt(0xFF32ADFF);
        RgbColor mDEFAULT_NORMAL_COLOR = RgbColor.fromArgbInt(0xFFFFFFFF);
        mBackgroundColors = AttrValue.get(attrSet, "action_item_badge",
            ColorListHelper.makeWithNormalAndSelect(mDEFAULT_NORMAL_COLOR, mDEFAULT_SELECTED_COLOR));
        getColorOfStateSet(new int[]{ComponentState.COMPONENT_STATE_SELECTED}, mBackgroundColors);
        getColorOfStateSet(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, mBackgroundColors);
        Logger.debug("mBackgroundColor " + mBackgroundColors + "mstroke " + mStroke + ", corners " + mCorners);
        StateElement stateElement = new StateElement();
        ShapeElement shapeElementSelect = new ShapeElement();
        shapeElementSelect.setRgbColor(RgbColor.fromArgbInt(mColorPressed));
        ShapeElement shapeElementNormal = new ShapeElement();
        shapeElementNormal.setRgbColor(RgbColor.fromArgbInt(mColor));
        if (mStroke > -1) {
            shapeElementNormal.setStroke(mStroke, RgbColor.fromArgbInt(mStrokeColor));
            shapeElementSelect.setStroke(mStroke, RgbColor.fromArgbInt(mStrokeColor));
        }
        if (mCorners > -1) {
            shapeElementNormal.setCornerRadius(mCorners);
            shapeElementSelect.setCornerRadius(mCorners);
        }
        stateElement.addState(new int[]{ComponentState.COMPONENT_STATE_SELECTED}, shapeElementSelect);
        stateElement.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, shapeElementNormal);
        return stateElement;
    }

    /**
     * 获取 ColorOfStateSet
     *
     * @param stateSet 颜色的下标
     * @param stateElement 状态element
     * @return RgbColor
     **/
    private RgbColor getColorOfStateSet(int[] stateSet, StateElement stateElement) {
        // 查找相应状态 element 的 index
        int index = stateElement.findStateElementIndex(stateSet);
        if (index < 0) {
            throw new IllegalArgumentException("stateSet not found!");
        }
        // 根据 index 取出 Element (这里是要取出颜色值，所以强转为 ShapeElement 再取)
        ShapeElement element = (ShapeElement) stateElement.getStateElement(index).getCurrentElement();
        return element.getRgbColors()[0];
    }

}
